package com.interview.ujiuye4;

class MyThreadA implements Runnable{
    private Object obj;//同步的锁对象
    public MyThreadA(Object obj){
        this.obj = obj;
    }
    @Override
    public void run() {
        methodA();
    }
    public void methodA(){
        System.out.println("线程A开始.....");
        synchronized (obj){
            try {
                obj.wait();
                //obj.wait(3000);//等待 Blocked阻塞  Timed_WAITING
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        System.out.println("线程A结束.....");
    }
}

class MyThreadB implements Runnable{
    private Object obj;
    public MyThreadB(Object obj){
        this.obj = obj;
    }
    @Override
    public void run() {
        methodB();
    }
    public void methodB(){
        System.out.println("线程B开始.....");
        synchronized (obj){
            try {
                Thread.sleep(3000);//休眠3秒
                obj.notify();  //唤醒等待的线程
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        System.out.println("线程B结束.....");
    }

}
public class WaitAndNotify {
    public static void main(String[] args) {
        Object obj = new Object();
        new Thread( new MyThreadA(obj) ).start();
        new Thread( new MyThreadB(obj) ).start();
    }
}
